<?php
/**
 * 错误处理量类
 * @author wudi
 *
 */
class doom_core_error {

    /**
     * 
     * @var int
     */
    public $code;

    /**
     * 
     * @var string
     */
    public $message;

    /**
     * 
     * @var string
     */
    public $file;

    public $line;

    public $trace;

    private $_hasError = FALSE;

    /**
     * 
     * @param mixed $code
     * @param string $message
     */
    public function set($code, $message = '') {
        if (is_int($code) || is_string($code)) {
            $callStack = &debug_backtrace();
            
            $this->code = $code;
            $this->message = $message;
            
            $firstTrace = array_shift($callStack);
            $this->file = $firstTrace['file'];
            $this->line = $firstTrace['line'];
            $this->trace = array();
            
            self::_callTrace2errTrace($callStack, $this->trace);
            unset($callStack);
        } else if (is_object($code)) {
            $this->code = $code->code;
            $this->message = $code->message;
            $this->file = $code->file;
            $this->line = $code->line;
            $this->trace = &$code->trace;
        }
        $this->_hasError = true;
    }

    /**
	 * 将debug_backtrace()的结果转换为doom_core_error的trace
	 * @param array $backTrace
	 * @param array $errTrace: 
	 */
    static private function _callTrace2errTrace(&$backTrace, &$errTrace) {
        if (is_array($backTrace) && is_array($errTrace)) {
            foreach ($backTrace as &$bTrace) {
                $errTrace[] = array(
                        'file' => $bTrace['file'], 
                        'line' => $bTrace['line'], 
                        'class' => $bTrace['class'], 
                        'function' => $bTrace['function'], 
                        'type' => $bTrace['type']);
            }
        }
    }
}